package First;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import First.Window;

public class Ask extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;


    JLabel jlnumber = new JLabel("学号：");
    JTextField jtnumber = new JTextField("",20);


    JButton buttonask = new JButton("查询");
    JButton buttonreturn = new JButton("返回");

    public Ask() {
        JPanel jpnumber = new JPanel();
        JPanel jpCno1 = new JPanel();
        JPanel jpCno2 = new JPanel();
        JPanel jpCno3 = new JPanel();
        JPanel jpCno4 = new JPanel();
        JPanel jpCno5 = new JPanel();
        JPanel jpCno6 = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpforbutton.add(buttonask);
        jpforbutton.add(buttonreturn);

        buttonask.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Connection conn = null;
                ResultSet res = null;
                Statement stat = null;

                String sql = "SELECT Sno,Cno,Grade FROM SC;";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");

                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                            "root",
                            "12345678");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    String num=new String(jtnumber.getText());
                    while (res.next())
                    {
                        //System.out.println(res.getString(1)+equals(jtnumber.getText()));
                        //判断条件，如果输入字符和学号一样，就打印信息
                        if (res.getString(1).equals(jtnumber.getText()))
                        {
                            Look_Ask look_ask=new Look_Ask(num);
                            break;
                        }
                    }
                }catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                finally{
                    try{
                        conn.close();
                    }catch(SQLException ar){
                        ar.printStackTrace();
                    }
                }
            }
        }
        );

        buttonreturn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });
        this.setTitle("查询学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpCno1);
        this.add(jpCno2);
        this.add(jpCno3);
        this.add(jpCno4);
        this.add(jpCno5);
        this.add(jpCno6);
        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}